package org.wkg.lesson09;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class MyThreadPool {
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue();

    public MyThreadPool(int ThreadSum) {
        if (ThreadSum <= 0) {
            throw new IllegalArgumentException("线程数量不可以小于0");
        }

        for (int i = 1; i <= ThreadSum; i++) {
            Thread thread = new Thread(() -> {
                while (true) {
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            },"Thread"+i);
            thread.start();
        }


    }


    public void submit(Runnable runnable) throws InterruptedException {
        if (runnable == null) {
            throw new IllegalArgumentException("任务不可以为空");
        }
        queue.put(runnable);
    }

}
